import sysDictItemApi, {DictItem} from "@/api/sys/SysDictItemApi";
import store from "@/store";
import {computed} from "vue";

class DictUtils {

    //缓存数据字典的值
    public cacheDict = () => {
        sysDictItemApi.getList().success(result => {
            store.commit("localStorage/dictItems", result.data);
        }).request();
    }

    public getDictLabel = (code: string, value: string): string | undefined => {
        let dictItems = computed<DictItem[]>(() => store.state.localStorage.dictItems);
        if (dictItems.value == undefined || dictItems.value.length == 0) {
            this.cacheDict();
        }
        return dictItems.value.filter(dictItem => dictItem.code === code && dictItem.value === value).at(0)?.label
    }

    public getSelectDictItemByCode(code: string): DictItem[] {
        let dictItems = computed<DictItem[]>(() => store.state.localStorage.dictItems);
        if (dictItems.value == undefined || dictItems.value.length == 0) {
            this.cacheDict();
        }
        return dictItems.value.filter(dictItem => dictItem.code === code)
    }
}

export default new DictUtils();